package com.yyplatform.admin.infra.model;

import com.yyplatform.admin.infra.exception.ErrorCode;

import lombok.Getter;
import lombok.Setter;

/**
 * Created on 2024-09-13
 */
@Setter
@Getter
public class RestResponse<T> {

    private int code;

    //    @JsonInclude(Include.NON_NULL)
    private String msg;

    private T data;

    public static <T> RestResponse<T> ok() {
        return new RestResponse<>();
    }


    public static <T> RestResponse<T> ok(T data) {
        RestResponse<T> res = new RestResponse<T>();
        res.setData(data);
        return res;
    }


    public static <T> RestResponse<T> of(int code, String msg) {
        RestResponse<T> res = new RestResponse<T>();
        res.code = code;
        res.msg = msg;
        return res;
    }


    public static <T> RestResponse<T> of(ErrorCode errorCode) {
        RestResponse<T> res = new RestResponse<T>();
        res.code = errorCode.getCode();
        res.msg = errorCode.getMsg();
        return res;
    }


    public static RestResponse<?> judge(boolean result) {
        if (result) {
            return ok();
        }
        return of(ErrorCode.UNKNOW_ERROR);
    }

}
